home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / DTS QT Utilities.Aug-95 / Projects & Test Apps / DigitizerShell / Mac Framework / MacFramework.c < prev    next >
Encoding:
Text File  |  1995-04-30  |  8.1 KB  |  377 lines  |  [TEXT/MPCC]

  1. /*
  2.     File:        MacFramework.c
  3.  
  4.     Contains:    Basic Macintosh Functions for window, menu handling and similar things for the 
  5.                     SG/vdig environment.
  6.  
  7.     Written by:    DTS
  8.  
  9.     Copyright:    © 1994-1995 by Apple Computer, Inc., all rights reserved.
  10.  
  11.     Change History (most recent first):
  12.  
  13.        <1>         4/25/95    khs        first file
  14.        
  15. */
  16.  
  17.  
  18. // INCLUDES
  19. #include <SegLoad.h>
  20. #include <ToolUtils.h>
  21. #include <Devices.h>
  22. #include <Fonts.h>
  23.  
  24. #include "DTSQTUtilities.h"
  25. #include "AppConfiguration.h"
  26. #include "MacFramework.h"
  27.  
  28.  
  29. // GLOBALS
  30. Boolean gQuitFlag = false;                                        // Flag that keeps track of termination state.
  31. unsigned long gWNEsleep = kWNEDefaultSleep;        // WaitNextEvent sleep time.
  32.  
  33.  
  34. // PURE MAC TOOLBOX FUNCTIONS
  35.  
  36. // ______________________________________________________________________
  37. void InitMacEnvironment(long nMasters)
  38. {
  39.     long i;
  40.     MaxApplZone();
  41.     
  42.     for(i = 0; i <nMasters; i++)
  43.         MoreMasters();
  44.     
  45.     InitGraf(&qd.thePort);
  46.     InitFonts();
  47.     InitWindows();
  48.     InitMenus();
  49.     FlushEvents(everyEvent, 0);
  50.     TEInit();
  51.     InitCursor();
  52.     InitDialogs(NULL);
  53. }
  54.  
  55.  
  56. // ______________________________________________________________________
  57. void InitStack(long extraStackSpace)
  58. {
  59.     Ptr size = GetApplLimit();
  60.     SetApplLimit(size - extraStackSpace);    // make room on the stack
  61. }
  62.  
  63.  
  64. // ______________________________________________________________________
  65. Boolean InitMenubar(void)
  66. {
  67.     Handle aMenuHandle = NULL;
  68.     
  69.     aMenuHandle = GetNewMBar(mMenubar); DebugAssert(aMenuHandle != NULL);
  70.     if(aMenuHandle == NULL)
  71.     {
  72.         ShowWarning("\pCould not find the Menubar resource!", 0);
  73.         return false;
  74.     }
  75.     
  76.     SetMenuBar(aMenuHandle);
  77.     DisposeHandle(aMenuHandle);  DebugAssert(MemError() == noErr);
  78.     
  79.     AddResMenu(GetMHandle(mApple), 'DRVR');
  80.  
  81.     DrawMenuBar();
  82.     return true;
  83. }
  84.  
  85.  
  86. // ______________________________________________________________________
  87. void HandleMenuCommand(long theMenuResult)
  88. {
  89.     short             aMenuID, aMenuItem;
  90.     Str255            daName;
  91.     WindowRef    whichWindow;
  92.     
  93.     aMenuID = HiWord(theMenuResult);
  94.     aMenuItem = LoWord(theMenuResult);
  95.     
  96.     switch(aMenuID)
  97.     {
  98.         // APPLE MENU
  99.         case mApple:
  100.             switch(aMenuItem)
  101.             {
  102.                 case iAbout:    // about box
  103.                     ShowAboutDialogBox();     
  104.                     break;
  105.                 
  106.                 default:     // Apple menu handling
  107.                     GetItem(GetMHandle(mApple), aMenuItem, daName);
  108.                     (void)OpenDeskAcc(daName);
  109.                     break;
  110.             } // end switch(aMenuItem)
  111.             break;
  112.  
  113.         // FILE MENU            
  114.         case mFile:
  115.             switch(aMenuItem)
  116.             {
  117.                 case iNew:
  118.                     {
  119.                         CreateSGEnviroment();
  120.                     }
  121.                     break;
  122.                 
  123.                 case iClose:
  124.                     {
  125.                         if( (whichWindow = FrontWindow() ) != NULL)
  126.                         {    
  127.                             if(IsAppWindow(whichWindow))
  128.                                 DoDestroyMovieWindow(whichWindow);
  129.                         }
  130.                     }
  131.                     break;
  132.  
  133.                 case iQuit:
  134.                     {
  135.                         gQuitFlag = true;
  136.                         break;
  137.                     }
  138.  
  139.             } // end switch(aMenuItem), mFile
  140.             break;
  141.     
  142.  
  143.     default:
  144.         HandleApplicationMenu(aMenuID, aMenuItem);
  145.         break;
  146.     } // end switch(aMenuID)
  147.     
  148.     HiliteMenu(0);
  149. }
  150.  
  151.  
  152. // ______________________________________________________________________
  153. void AdjustMenus(void)
  154. {
  155.     WindowRef             aWindow;
  156.     
  157.     aWindow = FrontWindow();
  158.  
  159.     if(aWindow != NULL)
  160.     {
  161.         // Enable the close entry of we have windows = movies.
  162.         EnableItem( GetMHandle(mFile), iClose);
  163.  
  164.     } // end if(aWindow != NULL)
  165.     else 
  166.     {
  167.         DisableItem(GetMHandle(mFile), iClose);
  168.         
  169.     }
  170.     
  171.     AdjustApplicationMenus();                    // fix any specific app menus as well.
  172. }
  173.  
  174.  
  175. // ______________________________________________________________________
  176. void MainEventLoop(void)
  177. {
  178.     EventRecord                         anEvent;
  179.     WindowRef                        whichWindow;
  180.     Boolean                                aMovieEvent;
  181.     short                                aWindowPart;
  182.     Rect                                    aRefreshArea;
  183.     Point                                    aPoint  = {100, 100};
  184.     
  185.     while(!gQuitFlag)
  186.     {
  187.         WaitNextEvent(everyEvent, &anEvent, gWNEsleep, NULL);
  188.         
  189. #ifdef USESIOUX
  190.         SIOUXHandleOneEvent(&anEvent);
  191. #endif USESIOUX
  192.  
  193.         AdjustMenus();
  194.         aMovieEvent = false;
  195.         
  196.         if( (whichWindow = FrontWindow() ) != NULL)
  197.             DoIdle(whichWindow);
  198.  
  199.         switch(anEvent.what)
  200.         {
  201.             case mouseDown:
  202.                 aWindowPart = FindWindow(anEvent.where, &whichWindow);
  203.  
  204.                 // Window related events:            
  205.                 switch(aWindowPart)
  206.                 {
  207.                     case inMenuBar:
  208.                         HandleMenuCommand(MenuSelect(anEvent.where));
  209.                         break;
  210.     
  211.                     case inContent:
  212.                         SelectWindow(whichWindow);
  213.                         HandleContentClick(whichWindow, &anEvent);
  214.                         break;
  215.                     
  216.                     case inDrag:
  217.                         DoDragWindow(whichWindow, &anEvent);
  218.                         break;
  219.                     
  220.                     case inGoAway:
  221.                         // if the window is closed, dispose the movie, the controller and the window
  222.                         if( TrackGoAway(whichWindow, anEvent.where) )
  223.                             DoDestroyMovieWindow(whichWindow);
  224.                         break;
  225.                 } // end switch(aWindowPart):
  226.                 break;
  227.  
  228.                 // System level events:
  229.                 case updateEvt:
  230.                     whichWindow = (WindowRef)anEvent.message;
  231.                     aRefreshArea = ((**(whichWindow->visRgn)).rgnBBox);
  232.                     DoUpdateWindow(whichWindow, &aRefreshArea);
  233.                     break;
  234.                     
  235.                 case keyDown:
  236.                 case autoKey:
  237.                     HandleKeyPress(&anEvent);
  238.                     break;
  239.                 
  240.                 case diskEvt:
  241.                     if(HiWord(anEvent.message) != noErr)
  242.                         (void)DIBadMount(aPoint, anEvent.message);
  243.                     break;
  244.                 
  245.                 case activateEvt:
  246.                     whichWindow = (WindowRef)anEvent.message;
  247.                     
  248.                      if ( IsAppWindow(whichWindow) )
  249.                     {
  250.                         DoActivateWindow(whichWindow, ((anEvent.modifiers & activeFlag) != 0 ));
  251.                     }
  252.                     break;
  253.                     
  254.                 case osEvt:
  255.                     switch(( anEvent.message > 24) & 0x00FF )        // get high byte of word
  256.                     {
  257.                         case suspendResumeMessage:
  258.                             if( FrontWindow() )
  259.                             {
  260.                                 DoActivateWindow(FrontWindow(), !((anEvent.message & resumeFlag) == 0));
  261.                             }
  262.                             break;
  263.                         
  264.                         case mouseMovedMessage:
  265.                             break;
  266.                     } // end switch(anEvent.message > 24) & 0x00FF)    
  267.                     break;
  268.                 
  269.                 case nullEvent:
  270.                     if(( whichWindow = FrontWindow() ) != NULL)
  271.                         DoIdle(whichWindow);
  272.                     break;
  273.         } // end switch(anEvent.what)
  274.     } // end while(!gQuitFlag)
  275. }
  276.  
  277.  
  278. // ______________________________________________________________________
  279. Boolean IsAppWindow(WindowRef theWindow)
  280. {
  281.     short aWindowKind;
  282.     
  283.     if (theWindow == NULL)
  284.         return false;
  285.     else
  286.     {
  287.         aWindowKind = ((WindowPeek)theWindow)->windowKind;
  288.         return ( (aWindowKind >= userKind) || (aWindowKind == dialogKind) );
  289.     }
  290. }
  291.  
  292.  
  293. // ______________________________________________________________________
  294. void HandleKeyPress(EventRecord *theEvent)
  295. {
  296.     char aKey;
  297.     
  298.     aKey = theEvent->message & charCodeMask;
  299.     
  300.     if(theEvent->modifiers & cmdKey)        // command key down?
  301.         HandleMenuCommand(MenuKey(aKey));
  302. }
  303.  
  304.  
  305. // ______________________________________________________________________
  306. void ShowAboutDialogBox(void)
  307. {
  308.     DialogPtr aDialog;
  309.     short         itemHit;
  310.     FontInfo    aFontInfo;
  311.     GrafPtr        aSavedPort;
  312.     
  313.     GetPort(&aSavedPort);
  314.     aDialog = GetNewDialog(kAboutBox, NULL, (WindowPtr) - 1L); DebugAssert(aDialog != NULL);
  315.     SetPort(aDialog);
  316.  
  317.     // Change font to Geneva, 9pt, bold, just for the sake of it...
  318.     TextFont(applFont); TextSize(9); TextFace(bold);
  319.     GetFontInfo(&aFontInfo);
  320.     
  321.     (*((DialogPeek)aDialog)->textH)->txFont = applFont;
  322.     (*((DialogPeek)aDialog)->textH)->txSize = 9;
  323.     (*((DialogPeek)aDialog)->textH)->lineHeight = aFontInfo.ascent + aFontInfo.descent + aFontInfo.leading;
  324.     (*((DialogPeek)aDialog)->textH)->fontAscent = aFontInfo.ascent;
  325.  
  326.     SetDialogDefaultItem(aDialog, 1);
  327.         
  328.     do
  329.     {
  330.         ModalDialog(NULL, &itemHit);
  331.     } while(itemHit != ok);
  332.     
  333.     SetPort(aSavedPort);
  334.     DisposeDialog(aDialog);  DebugAssert(MemError() == noErr);
  335. }
  336.  
  337.  
  338. // ______________________________________________________________________
  339. void ShowWarning(Str255 theMessage, OSErr theErr)
  340. {
  341.     Str255 errString;
  342.     
  343.     NumToString(theErr, errString);
  344.     ParamText("\pWarning!", theMessage, theErr ? errString:  NULL, NULL);
  345.     Alert(kAlertError, NULL);
  346. }
  347.  
  348.  
  349. // ______________________________________________________________________
  350. void DoDestroyMovieWindow(WindowRef theWindow)
  351. {
  352.     DoCloseWindow(theWindow);
  353.  
  354.     DisposeWindow(theWindow); DebugAssert(MemError() == noErr);
  355.     
  356.     CompactMem(0xFFFFFFFF);        //We might as well compact the mem here for getting better performance later.
  357. }
  358.  
  359.  
  360. // ______________________________________________________________________
  361. void DoActivateWindow(WindowRef theWindow, Boolean becomingActive)
  362. {
  363.     WindowObject         aWindowObject = NULL;
  364.     MovieController    mc = NULL;
  365.     GrafPtr                    aSavedPort = NULL;
  366.     
  367.     GetPort(&aSavedPort);
  368.     SetPort((GrafPtr)theWindow);
  369.     
  370.     // @@@ Do something related to activation of movie here.
  371.     
  372.     SetPort(aSavedPort);
  373. }
  374.  
  375.  
  376.  
  377.